package com.digoyo.hr.service;

import com.digoyo.hr.mapper.MenuMapper;
import com.digoyo.hr.mapper.MenuRoleMapper;
import com.digoyo.hr.model.Hr;
import com.digoyo.hr.model.Menu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author: Daniel
 * @Description:
 * @Date: Created in 3:00 PM 2020/3/24
 * @Modified By:
 */

@Service
public class MenuService {

    @Autowired
    MenuMapper menuMapper;
    @Autowired
    MenuRoleMapper menuRoleMapper;

    @Autowired
    HrService hrService;

    public List<Menu> getMenusForCurrentHr() {
        Hr hr = hrService.getCurrentHr();
        return menuMapper.getMenusByHrId(hr.getId());
    }

    public List<Menu> getAllMenusWithRole(){
        return menuMapper.getAllMenusWithRole();
    }

    public List<Menu> getAllMenus() {
        return menuMapper.getAllMenus();
    }

    public List<Integer> getMidsByRid(Integer rid) {
        return menuMapper.getMidsByRid(rid);
    }
    @Transactional
    public boolean updateMenuRole(Integer rid, Integer[] mids) {
        menuRoleMapper.deleteByRid(rid);
        if (mids!=null&&mids.length>0) {
            Integer result = menuRoleMapper.insertRecord(rid,mids);
            return result==mids.length;
        }
        return true;
    }
}
